﻿using Future.Common.Attribute;
using Future.Common.Exception;
using Future.Common.Models;
using Future.IBLL;
using Future.IDAL;
using Future.Model.DtoModel;
using Future.Model.SysModel;

namespace Future.BLL
{
    /// <summary>
    /// 描述：角色业务逻辑
    /// 创建人： yangkai
    /// 创建时间：2022/12/5 16:38:39
    /// </summary>
    public class RoleBLL : IRoleBLL
    {
        private readonly IRoleDAL _roleDAL;
        private readonly IRoleMenuDAL _roleMenuDAL;
        private readonly IUserRoleDAL _userRoleDAL;

        public RoleBLL(IRoleDAL roleDAL, IRoleMenuDAL roleMenuDAL, IUserRoleDAL userRoleDAL)
        {
            _roleDAL = roleDAL;
            _roleMenuDAL = roleMenuDAL;
            _userRoleDAL = userRoleDAL;
        }

        [Require]
        public bool AddRoleInfo(RoleInfoDto roleDto)
        {
            RoleModel existRole = _roleDAL.Get(x => x.RoleName == roleDto.Role.RoleName || x.RoleCode == roleDto.Role.RoleCode);
            if (existRole != null)
            {
                throw new BusinessException("角色名称或权限字符已经存在");
            }

            RoleModel roleModel = roleDto.Role;
            roleModel.Id = Guid.NewGuid();
            _roleDAL.Insert(roleModel);
            GiveRoleSetMenu(roleModel.Id, roleDto.MenuIds);

            return true;
        }

        /// <summary>
        /// 角色配置菜单权限
        /// </summary>
        /// <param name="roleId"></param>
        /// <param name="menuIds"></param>
        private void GiveRoleSetMenu(Guid roleId, IEnumerable<Guid> menuIds)
        {
            _roleMenuDAL.Delete(x => x.RoleId == roleId);

            if (!menuIds.Any()) return;
            List<RoleMenuModel> menuModels = new();
            foreach (Guid menuId in menuIds)
            {
                RoleMenuModel roleMenuModel = new()
                {
                    Id = Guid.NewGuid(),
                    RoleId = roleId,
                    MenuId = menuId
                };
                menuModels.Add(roleMenuModel);
            }
            _roleMenuDAL.InsertMany(menuModels);
        }

        public PageModel<RoleModel> SelctPageList(RoleQueryDto roleQueryDto, PageParModel page)
        {
            PageModel<RoleModel> result = _roleDAL.SelctPageList(roleQueryDto, page);
            return result;
        }

        public RoleModel GetById(Guid id)
        {
            return _roleDAL.Get(x => x.Id == id);
        }

        [Require]
        public bool UpdateInfo(RoleInfoDto roleDto)
        {
            RoleModel roleModel = _roleDAL.Get(x => x.Id == roleDto.Role.Id);
            if (roleModel == null)
            {
                throw new BusinessException("角色不存在");
            }
            RoleModel existRole = _roleDAL.Get(x => x.RoleName == roleDto.Role.RoleCode || x.RoleCode == roleDto.Role.RoleCode);
            if (existRole != null && existRole.Id != roleModel.Id)
            {
                throw new BusinessException("角色名称或权限字符已经存在");
            }

            roleModel.OrderNum = roleDto.Role.OrderNum;
            roleModel.RoleName = roleDto.Role.RoleName;
            roleModel.Remark = roleDto.Role.Remark;
            roleModel.RoleCode = roleDto.Role.RoleCode;
            roleModel.Status = roleDto.Role.Status;

            _roleDAL.Update(roleModel);
            GiveRoleSetMenu(roleModel.Id, roleDto.MenuIds);
            return true;
        }

        public bool DeleteByIds(List<Guid> ids)
        {
            _roleDAL.Delete(x => ids.Contains(x.Id));
            _roleMenuDAL.Delete(x => ids.Contains(x.RoleId));

            return true;
        }

        public List<RoleModel> GetList()
        {
            return _roleDAL.GetList(x => x.Status);
        }

        public bool AuthUserList(Guid roleId, List<Guid> userIds)
        {
            List<UserRoleModel> userRoleModels = new();
            foreach (Guid userId in userIds)
            {
                UserRoleModel roleModel = new()
                {
                    Id = Guid.NewGuid(),
                    UserId = userId,
                    RoleId = roleId
                };
                userRoleModels.Add(roleModel);
            }
            return _userRoleDAL.InsertMany(userRoleModels);
        }

        public bool CancelAuthUserList(Guid roleId, List<Guid> userIds)
        {
            return _userRoleDAL.Delete(x => x.RoleId == roleId && userIds.Contains(x.UserId));
        }

        public bool UpdateStatus(Guid roleId, bool status)
        {
            RoleModel roleModel = _roleDAL.Get(x => x.Id == roleId);
            if (roleModel == null)
            {
                throw new BusinessException("角色不存在");
            }

            roleModel.Status = status;
            return _roleDAL.Update(roleModel);
        }
    }
}
